﻿using Dting_libraries;
using OfficeOpenXml;
using Station_interface;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace Seek_adviceFrom
{
    public partial class Seek_adviceFrom : UserControl
    {
        #region //属性
        public mySql_class _mySql_class { get; set; }

        #endregion


        public object Seek_mySql_Class;
        public Seek_adviceFrom()
        {
            InitializeComponent();
            SetStyle(ControlStyles.ResizeRedraw | ControlStyles.AllPaintingInWmPaint | ControlStyles.OptimizedDoubleBuffer, true);
            UpdateStyles();

            //优化dataGridView控件闪耀现象
            Type dgvType = dataGridView1.GetType();
            PropertyInfo pi = dgvType.GetProperty("DoubleBuffered", BindingFlags.Instance | BindingFlags.NonPublic);
            pi.SetValue(dataGridView1, true, null);

            //timer1.Interval = 1;
            //timer1.Enabled = true;
        }

        protected override CreateParams CreateParams
        {
            get
            {
                CreateParams cp = base.CreateParams;
                cp.ExStyle |= 0x02000000;
                //cp.ExStyle &= ~0x02000000;
                return cp;
            }
        }

        private void textBox1_MouseClick(object sender, MouseEventArgs e)
        {
            textBox1.Text = string.Empty;
            textBox3.Text = string.Empty;
            textBox3.ReadOnly = true;
            textBox1.ReadOnly = false;
        }

        private void textBox3_MouseClick(object sender, MouseEventArgs e)
        {
            textBox1.Text = string.Empty;
            textBox3.Text = string.Empty;
            textBox3.ReadOnly = false;
            textBox1.ReadOnly = true;
        }

        private void TextBox1_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                materialButton1_Click(null ,e);
            }
        }

        private void TextBox3_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == '\r')
            {
                materialButton1_Click(null, e);
            }
        }


        //开始搜索数据
        private void materialButton1_Click(object sender, EventArgs e)
        {
            int station = 0;
            int.TryParse(textBox2.Text, out station);

            string start_date = dateTimePicker1.Value.ToString("yyyy/MM/dd ") + dateTimePicker4.Value.ToString("HH/mm");
            string enf_date = dateTimePicker2.Value.ToString("yyyy/MM/dd ") + dateTimePicker3.Value.ToString("HH/mm");
            if (radioButton3.Checked == true)//单工位
            {
                Task _task = Task.Run(() =>
                {
                    this.SafeInvoke(() =>
                    {
                        one_seek(station);
                    });
                });
            }
            else if (radioButton4.Checked == true)//全部工位
            {
                Task _task = Task.Run(() =>
                {
                    this.SafeInvoke(() =>
                    {
                        all_seek();
                    });
                });
            }
            else if (radioButton1.Checked == true)//按时间
            {
                Task _task = Task.Run(() =>
                {
                    this.SafeInvoke(() =>
                    {
                        date_seek(station, start_date, enf_date);
                    });
                });
            }
            else if (radioButton2.Checked == true)//查找当前条码生产状态
            {
                Task _task = Task.Run(() =>
                {
                    this.SafeInvoke(() =>
                    {
                        one_seek();
                    });
                });
            }
            else if (radioButton5.Checked == true)//查找NG
            {
                Task _task = Task.Run(() =>
                {
                    this.SafeInvoke(() =>
                    {
                        date_Ngseek(station, start_date, enf_date);
                    });
                });
            }
            else
            {
                MessageBox.Show("请选择单工位、全部工位、或时间。", "提示");
            }
        }
        //导出数据
        private void materialButton2_Click(object sender, EventArgs e)
        {
            export();
        }

        //搜索数据函数
        private void all_seek()
        {
            try
            {
                var bufMySql = _mySql_class;
                if (bufMySql == null) return;
                string roi_str = textBox1.Text;
                string roi_str2 = textBox3.Text;
                if (roi_str == string.Empty && roi_str2 == string.Empty)
                {
                    MessageBox.Show("码为空，请输入条码", "提示"); return;
                }
                if (roi_str2 != "")
                {
                    roi_str = roi_str2;
                }
                //通过8工位二维码码获取条码 
                if (roi_str2 != "" && roi_str2 != string.Empty)
                {
                    string _arry = bufMySql.Mysqllist_Seek2("p8_station", "刻印二维码信息", roi_str2, "Bar_sn");
                    if (_arry != string.Empty)
                    {
                        roi_str = _arry;
                    }
                }

                //获取每张表的字段名称
                string[] P1_NameArry = bufMySql.MysqlField_Seek("droduce_data", "p1_station");
                string[] P2_NameArry = bufMySql.MysqlField_Seek("droduce_data", "p2_station");
                string[] P3_NameArry = bufMySql.MysqlField_Seek("droduce_data", "p3_station");
                string[] P4_NameArry = bufMySql.MysqlField_Seek("droduce_data", "p4_station");
                string[] P5_NameArry = bufMySql.MysqlField_Seek("droduce_data", "p5_station");
                string[] P6_NameArry = bufMySql.MysqlField_Seek("droduce_data", "p6_station");
                string[] P7_NameArry = bufMySql.MysqlField_Seek("droduce_data", "p7_station");
                string[] P8_NameArry = bufMySql.MysqlField_Seek("droduce_data", "p8_station");
                ;
                //获取每张表指定条码的数据
                string[] P1_arry = null;
                string[] P2_arry = null;
                string[] P3_arry = null;
                string[] P4_arry = null;
                string[] P5_arry = null;
                string[] P6_arry = null;
                string[] P7_arry = null;
                string[] P8_arry = null;
                if (checkBox1.Checked)
                {
                    P1_arry = bufMySql.Mysqllist_Seek3("p1_station", "生产条码", roi_str, "产品型号", comboBox1.Text);
                    P2_arry = bufMySql.Mysqllist_Seek3("p2_station", "生产条码", roi_str, "产品型号", comboBox1.Text);
                    P3_arry = bufMySql.Mysqllist_Seek3("p3_station", "生产条码", roi_str, "产品型号", comboBox1.Text);
                    P4_arry = bufMySql.Mysqllist_Seek3("p4_station", "生产条码", roi_str, "产品型号", comboBox1.Text);
                    P5_arry = bufMySql.Mysqllist_Seek3("p5_station", "生产条码", roi_str, "产品型号", comboBox1.Text);
                    P6_arry = bufMySql.Mysqllist_Seek3("p6_station", "生产条码", roi_str, "产品型号", comboBox1.Text);
                    P7_arry = bufMySql.Mysqllist_Seek3("p7_station", "生产条码", roi_str, "产品型号", comboBox1.Text);
                    P8_arry = bufMySql.Mysqllist_Seek3("p8_station", "生产条码", roi_str, "产品型号", comboBox1.Text);
                }
                else
                {
                    P1_arry = bufMySql.Mysqllist_Seek("p1_station", "生产条码", roi_str);
                    P2_arry = bufMySql.Mysqllist_Seek("p2_station", "生产条码", roi_str);
                    P3_arry = bufMySql.Mysqllist_Seek("p3_station", "生产条码", roi_str);
                    P4_arry = bufMySql.Mysqllist_Seek("p4_station", "生产条码", roi_str);
                    P5_arry = bufMySql.Mysqllist_Seek("p5_station", "生产条码", roi_str);
                    P6_arry = bufMySql.Mysqllist_Seek("p6_station", "生产条码", roi_str);
                    P7_arry = bufMySql.Mysqllist_Seek("p7_station", "生产条码", roi_str);
                    P8_arry = bufMySql.Mysqllist_Seek("p8_station", "生产条码", roi_str);
                }

                //添加表格
                string[] title_str = new string[8] { "P1_工位", "P2_工位", "P3_工位", "P4_工位", "P5_工位", "P6_工位", "P7_工位", "P8_工位" };
                int[] title_width = new int[8] { 230, 230, 230, 230, 230, 230, 230, 230 };
                DataGridView_init(dataGridView1, title_str, title_width);

                if (P1_NameArry == null || P2_NameArry == null || P3_NameArry == null || P4_NameArry == null ||
                    P5_NameArry == null || P6_NameArry == null || P7_NameArry == null || P8_NameArry == null)
                {
                    MessageBox.Show("字段名获取错误", "提示");
                }
                int[] max_arry = new int[8] { P1_NameArry.Length, P2_NameArry.Length, P3_NameArry.Length, P4_NameArry.Length,
                                          P5_NameArry.Length,P6_NameArry.Length,P7_NameArry.Length,P8_NameArry.Length};
                int max_length = max_arry.Max();

                //添加表格数据
                dataGridView1.Rows.Clear();
                for (int h = 0; h < 9; h++)
                {
                    for (int i = 0; i < max_length; i++)
                    {
                        if (h < 1)
                        { dataGridView1.Rows.Add(); dataGridView1.Rows.Add(); }

                        string value1 = "";
                        string value2 = "";

                        switch (h)
                        {
                            case 1:
                                try { value1 = (P1_NameArry != null && P1_NameArry[i] != null) ? P1_NameArry[i] : ""; } catch { value1 = ""; };
                                try { value2 = (P1_arry != null && P1_arry[i] != null) ? P1_arry[i] : ""; } catch { value2 = ""; };
                                dataGridView1.Rows[(i * 2) + 0].Cells[h].Value = value1;
                                dataGridView1.Rows[(i * 2) + 1].Cells[h].Value = value2;
                                break;
                            case 2:
                                try { value1 = (P2_NameArry != null && P2_NameArry[i] != null) ? P2_NameArry[i] : ""; } catch { value1 = ""; };
                                try { value2 = (P2_arry != null && P2_arry[i] != null) ? P2_arry[i] : ""; } catch { value2 = ""; };
                                dataGridView1.Rows[(i * 2) + 0].Cells[h].Value = value1;
                                dataGridView1.Rows[(i * 2) + 1].Cells[h].Value = value2;
                                break;
                            case 3:
                                try { value1 = (P3_NameArry != null && P3_NameArry[i] != null) ? P3_NameArry[i] : ""; } catch { value1 = ""; };
                                try { value2 = (P3_arry != null && P3_arry[i] != null) ? P3_arry[i] : ""; } catch { value2 = ""; };
                                dataGridView1.Rows[(i * 2) + 0].Cells[h].Value = value1;
                                dataGridView1.Rows[(i * 2) + 1].Cells[h].Value = value2;
                                break;
                            case 4:
                                try { value1 = (P4_NameArry != null && P4_NameArry[i] != null) ? P4_NameArry[i] : ""; } catch { value1 = ""; };
                                try { value2 = (P4_arry != null && P4_arry[i] != null) ? P4_arry[i] : ""; } catch { value2 = ""; };
                                dataGridView1.Rows[(i * 2) + 0].Cells[h].Value = value1;
                                dataGridView1.Rows[(i * 2) + 1].Cells[h].Value = value2;
                                break;
                            case 5:
                                try { value1 = (P5_NameArry != null && P5_NameArry[i] != null) ? P5_NameArry[i] : ""; } catch { value1 = ""; };
                                try { value2 = (P5_arry != null && P5_arry[i] != null) ? P5_arry[i] : ""; } catch { value2 = ""; };
                                dataGridView1.Rows[(i * 2) + 0].Cells[h].Value = value1;
                                dataGridView1.Rows[(i * 2) + 1].Cells[h].Value = value2;
                                break;
                            case 6:
                                try { value1 = (P6_NameArry != null && P6_NameArry[i] != null) ? P6_NameArry[i] : ""; } catch { value1 = ""; };
                                try { value2 = (P6_arry != null && P6_arry[i] != null) ? P6_arry[i] : ""; } catch { value2 = ""; };
                                dataGridView1.Rows[(i * 2) + 0].Cells[h].Value = value1;
                                dataGridView1.Rows[(i * 2) + 1].Cells[h].Value = value2;
                                break;
                            case 7:
                                try { value1 = (P7_NameArry != null && P7_NameArry[i] != null) ? P7_NameArry[i] : ""; } catch { value1 = ""; };
                                try { value2 = (P7_arry != null && P7_arry[i] != null) ? P7_arry[i] : ""; } catch { value2 = ""; };
                                dataGridView1.Rows[(i * 2) + 0].Cells[h].Value = value1;
                                dataGridView1.Rows[(i * 2) + 1].Cells[h].Value = value2;
                                break;
                            case 8:
                                try { value1 = (P8_NameArry != null && P8_NameArry[i] != null) ? P8_NameArry[i] : ""; } catch { value1 = ""; };
                                try { value2 = (P8_arry != null && P8_arry[i] != null) ? P8_arry[i] : ""; } catch { value2 = ""; };
                                dataGridView1.Rows[(i * 2) + 0].Cells[h].Value = value1;
                                dataGridView1.Rows[(i * 2) + 1].Cells[h].Value = value2;
                                break;
                        }


                        //this.dataGridView1.Update();
                        //Application.DoEvents();
                    }
                }
            }
            catch (Exception er)
            {
                MessageBox.Show("错误信息：" + er.ToString(), "提示");
            }
            ;
        }

        private void one_seek(int station)
        {
            try
            {
                var bufMySql = _mySql_class;
                if (bufMySql == null) return;
                string roi_str = textBox1.Text;
                string roi_str2 = textBox3.Text;
                if (roi_str == string.Empty && roi_str2 == string.Empty)
                {
                    MessageBox.Show("码为空，请输入条码", "提示"); return;
                }
                if (roi_str2!="")
                {
                    roi_str = roi_str2;
                }
                //通过8工位二维码码获取条码 
                if (roi_str2 != "" && roi_str2 != string.Empty)
                {
                    string _arry = bufMySql.Mysqllist_Seek2("p8_station", "刻印二维码信息", roi_str2, "生产条码");
                    if (_arry != string.Empty)
                    {
                        roi_str = _arry;
                    }
                }



                switch (station)
                {
                    case 1:
                        //获取每张表的字段名称
                        string[] P1_NameArry = bufMySql.MysqlField_Seek("droduce_data", "p1_station");
                        //获取每张表指定条码的数据
                        string[] P1_arry = null;
                        if (checkBox1.Checked)
                        {
                            P1_arry = bufMySql.Mysqllist_Seek3("p1_station", "生产条码", roi_str, "产品型号", comboBox1.Text);
                        }
                        else
                        {
                            P1_arry = bufMySql.Mysqllist_Seek("p1_station", "生产条码", roi_str);
                        }
                        //添加表格
                        string[] title_str = new string[2] { "P1_工位", "参数值" };
                        int[] title_width = new int[2] { 230, 230 };
                        DataGridView_init(dataGridView1, title_str, title_width);
                        //添加表格数据
                        DataGridView(P1_NameArry, P1_arry);
                        break;
                    case 2:
                        //获取每张表的字段名称
                        string[] P2_NameArry = bufMySql.MysqlField_Seek("droduce_data", "p2_station");
                        //获取每张表指定条码的数据
                        string[] P2_arry = null;
                        if (checkBox1.Checked)
                        {
                            P2_arry = bufMySql.Mysqllist_Seek3("p2_station", "生产条码", roi_str, "产品型号", comboBox1.Text);
                        }
                        else
                        {
                            P2_arry = bufMySql.Mysqllist_Seek("p2_station", "生产条码", roi_str);
                        }
                        //添加表格
                        title_str = new string[2] { "P2_工位", "参数值" };
                        title_width = new int[2] { 230, 230 };
                        DataGridView_init(dataGridView1, title_str, title_width);
                        //添加表格数据
                        DataGridView(P2_NameArry, P2_arry);
                        break;
                    case 3:
                        //获取每张表的字段名称
                        string[] P3_NameArry = bufMySql.MysqlField_Seek("droduce_data", "p3_station");
                        //获取每张表指定条码的数据
                        string[] P3_arry = null;
                        if (checkBox1.Checked)
                        {
                            P3_arry = bufMySql.Mysqllist_Seek3("p3_station", "生产条码", roi_str, "产品型号", comboBox1.Text);
                        }
                        else
                        {
                            P3_arry = bufMySql.Mysqllist_Seek("p3_station", "生产条码", roi_str);
                        }
                        //添加表格
                        title_str = new string[2] { "P3_工位", "参数值" };
                        title_width = new int[2] { 230, 230 };
                        DataGridView_init(dataGridView1, title_str, title_width);
                        //添加表格数据
                        DataGridView(P3_NameArry, P3_arry);
                        break;
                    case 4:
                        //获取每张表的字段名称
                        string[] P4_NameArry = bufMySql.MysqlField_Seek("droduce_data", "p4_station");
                        //获取每张表指定条码的数据
                        string[] P4_arry = null;
                        if (checkBox1.Checked)
                        {
                            P4_arry = bufMySql.Mysqllist_Seek3("p4_station", "生产条码", roi_str, "产品型号", comboBox1.Text);
                        }
                        else
                        {
                            P4_arry = bufMySql.Mysqllist_Seek("p4_station", "生产条码", roi_str);
                        }
                        //添加表格
                        title_str = new string[2] { "P4_工位", "参数值" };
                        title_width = new int[2] { 230, 230 };
                        DataGridView_init(dataGridView1, title_str, title_width);
                        //添加表格数据
                        DataGridView(P4_NameArry, P4_arry);
                        break;
                    case 5:
                        //获取每张表的字段名称
                        string[] P5_NameArry = bufMySql.MysqlField_Seek("droduce_data", "p5_station");
                        //获取每张表指定条码的数据
                        string[] P5_arry = null;
                        if (checkBox1.Checked)
                        {
                            P5_arry = bufMySql.Mysqllist_Seek3("p5_station", "生产条码", roi_str, "产品型号", comboBox1.Text);
                        }
                        else
                        {
                            P5_arry = bufMySql.Mysqllist_Seek("p5_station", "生产条码", roi_str);
                        }
                        //添加表格
                        title_str = new string[2] { "P5_工位", "参数值" };
                        title_width = new int[2] { 230, 230 };
                        DataGridView_init(dataGridView1, title_str, title_width);
                        //添加表格数据
                        DataGridView(P5_NameArry, P5_arry);
                        break;
                    case 6:
                        //获取每张表的字段名称
                        string[] P6_NameArry = bufMySql.MysqlField_Seek("droduce_data", "p6_station");
                        //获取每张表指定条码的数据
                        string[] P6_arry = null;
                        if (checkBox1.Checked)
                        {
                            P6_arry = bufMySql.Mysqllist_Seek3("p6_station", "生产条码", roi_str, "产品型号", comboBox1.Text);
                        }
                        else
                        {
                            P6_arry = bufMySql.Mysqllist_Seek("p6_station", "生产条码", roi_str);
                        }
                        //添加表格
                        title_str = new string[2] { "P6_工位", "参数值" };
                        title_width = new int[2] { 230, 230 };
                        DataGridView_init(dataGridView1, title_str, title_width);
                        //添加表格数据
                        DataGridView(P6_NameArry, P6_arry);
                        break;
                    case 7:
                        //获取每张表的字段名称
                        string[] P7_NameArry = bufMySql.MysqlField_Seek("droduce_data", "p7_station");
                        //获取每张表指定条码的数据
                        string[] P7_arry = null;
                        if (checkBox1.Checked)
                        {
                            P7_arry = bufMySql.Mysqllist_Seek3("p7_station", "生产条码", roi_str, "产品型号", comboBox1.Text);
                        }
                        else
                        {
                            P7_arry = bufMySql.Mysqllist_Seek("p7_station", "生产条码", roi_str);
                        }
                        //添加表格
                        title_str = new string[2] { "P7_工位", "参数值" };
                        title_width = new int[2] { 230, 230 };
                        DataGridView_init(dataGridView1, title_str, title_width);
                        //添加表格数据
                        DataGridView(P7_NameArry, P7_arry);
                        break;
                    case 8:
                        //获取每张表的字段名称
                        string[] P8_NameArry = bufMySql.MysqlField_Seek("droduce_data", "p8_station");
                        //获取每张表指定条码的数据
                        string[] P8_arry = null;
                        if (checkBox1.Checked)
                        {
                           P8_arry = bufMySql.Mysqllist_Seek3("p8_station", "生产条码", roi_str, "产品型号", comboBox1.Text);
                        }
                        else
                        {
                            P8_arry = bufMySql.Mysqllist_Seek("p8_station", "生产条码", roi_str);
                        }
                        //添加表格
                        title_str = new string[2] { "P8_工位", "参数值" };
                        title_width = new int[2] { 230, 230 };
                        DataGridView_init(dataGridView1, title_str, title_width);
                        //添加表格数据
                        DataGridView(P8_NameArry, P8_arry);
                        break;
                }
            }
            catch (Exception er)
            {
                MessageBox.Show("错误信息：" + er.ToString(), "提示");
            }
            ;
        }

        private void date_seek(int station, string start_date,string end_date)
        {
            try
                {
                var bufMySql = _mySql_class;
                if (bufMySql == null) return;
                DataTable buf_datatable = null;
                string[] P_NameArry = null;
                string[] is_NameArry = new string[2] { "产品型号", comboBox1.Text };

                switch (station)
                {
                    case 1:
                        P_NameArry = bufMySql.MysqlField_Seek("droduce_data", "p1_station");
                        if (checkBox1.Checked)
                            buf_datatable = bufMySql.MysqlDate_seek("p1_station", "生产日期", is_NameArry, start_date, end_date);
                        else
                            buf_datatable = bufMySql.MysqlDate_seek("p1_station", "生产日期", start_date, end_date);
                        break;
                    case 2:
                        P_NameArry = bufMySql.MysqlField_Seek("droduce_data", "p2_station");
                        if (checkBox1.Checked)
                            buf_datatable = bufMySql.MysqlDate_seek("p2_station", "生产日期", is_NameArry, start_date, end_date);
                        else
                            buf_datatable = bufMySql.MysqlDate_seek("p2_station", "生产日期", start_date, end_date);
                        break;
                    case 3:
                        P_NameArry = bufMySql.MysqlField_Seek("droduce_data", "p3_station");
                        if (checkBox1.Checked)
                            buf_datatable = bufMySql.MysqlDate_seek("p3_station", "生产日期", is_NameArry, start_date, end_date);
                        else
                            buf_datatable = bufMySql.MysqlDate_seek("p3_station", "生产日期", start_date, end_date);
                        break;
                    case 4:
                        P_NameArry = bufMySql.MysqlField_Seek("droduce_data", "p4_station");
                        if (checkBox1.Checked)
                            buf_datatable = bufMySql.MysqlDate_seek("p4_station", "生产日期", is_NameArry, start_date, end_date);
                        else
                            buf_datatable = bufMySql.MysqlDate_seek("p4_station", "生产日期", start_date, end_date);
                        break;
                    case 5:
                        P_NameArry = bufMySql.MysqlField_Seek("droduce_data", "p5_station");
                        if (checkBox1.Checked)
                            buf_datatable = bufMySql.MysqlDate_seek("p5_station", "生产日期", is_NameArry, start_date, end_date);
                        else
                            buf_datatable = bufMySql.MysqlDate_seek("p5_station", "生产日期", start_date, end_date);
                        break;
                    case 6:
                        P_NameArry = bufMySql.MysqlField_Seek("droduce_data", "p6_station");
                        if (checkBox1.Checked)
                            buf_datatable = bufMySql.MysqlDate_seek("p6_station", "生产日期", is_NameArry, start_date, end_date);
                        else
                            buf_datatable = bufMySql.MysqlDate_seek("p6_station", "生产日期", start_date, end_date);
                        break;
                    case 7:
                        P_NameArry = bufMySql.MysqlField_Seek("droduce_data", "p7_station");
                        if (checkBox1.Checked)
                            buf_datatable = bufMySql.MysqlDate_seek("p7_station", "生产日期", is_NameArry, start_date, end_date);
                        else
                            buf_datatable = bufMySql.MysqlDate_seek("p7_station", "生产日期", start_date, end_date);
                        break;
                    case 8:
                        P_NameArry = bufMySql.MysqlField_Seek("droduce_data", "p8_station");
                        if (checkBox1.Checked)
                            buf_datatable = bufMySql.MysqlDate_seek("p8_station", "生产日期", is_NameArry, start_date, end_date);
                        else
                            buf_datatable = bufMySql.MysqlDate_seek("p8_station", "生产日期", start_date, end_date);
                        break;
                }

                //显示列名
                DataGridView_init2(dataGridView1, P_NameArry);
                //显示数据
                GridviewShowDataTable(buf_datatable, dataGridView1);
            }
            catch (Exception er)
            {
                MessageBox.Show("错误信息："+er.ToString(),"提示");
            }
        }

        private void one_seek()
        {
            try
            {
                var bufMySql = _mySql_class;
                if (bufMySql == null) return;
                string roi_str = textBox1.Text;
                string roi_str2 = textBox3.Text;
                if (roi_str == string.Empty && roi_str2 == string.Empty)
                {
                    MessageBox.Show("码为空，请输入条码", "提示"); return;
                }


                //获取表的字段名称
                string[] P1_NameArry = bufMySql.MysqlField_Seek("droduce_data", "main_station_data_buf");
                //获取每张表指定条码的数据
                string[] P1_arry = bufMySql.Mysqllist_Seek("main_station_data_buf", "生产条码", roi_str);
                //添加表格
                string[] title_str = new string[2] { "当前条码", "生产状态" };
                int[] title_width = new int[2] { 230, 1300 };
                DataGridView_init(dataGridView1, title_str, title_width);
                if (P1_arry!=null && P1_arry.Length >0)
                {
                    string str_buf = "";
                    string[] str_buf2 = P1_arry[2].Split(new char[1] { '&'});

                    for (int i = 0; i < str_buf2.Length; i++)
                    {
                        if (str_buf2[i]!=string.Empty)
                        {
                            str_buf += "P" + str_buf2[i] + "工位&";
                        }
                    }
                    P1_arry[2] = str_buf;
                    ;
                }
                //添加表格数据
                DataGridView2(P1_NameArry, P1_arry);


 
            }
            catch (Exception er)
            {
                MessageBox.Show("错误信息：" + er.ToString(), "提示");
            }
            ;
        }

        private void date_Ngseek(int station, string start_date, string end_date)
        {
            try
            {
                var bufMySql = _mySql_class;
                if (bufMySql == null) return;
                DataTable buf_datatable = null;
                string[] P_NameArry = null;
                string[] bufstr = null;
                string roi_str = textBox2.Text;
                if (roi_str == string.Empty)
                {
                    MessageBox.Show("码为空，请输入条码", "提示"); return;
                }

                string[] is_NameArry = new string[4] { "综合判定结果", "NG","产品型号", comboBox1.Text };
                bufstr = new string[2] { "综合判定结果", "NG" };

                switch (station)
                {
                    case 1:
                        P_NameArry = bufMySql.MysqlField_Seek("droduce_data", "p1_station");
                        if (checkBox1.Checked)
                            buf_datatable = bufMySql.MysqlDate_seek("p1_station", "生产日期", is_NameArry, start_date, end_date);
                        else
                            buf_datatable = bufMySql.MysqlDate_seek("p1_station", "生产日期", bufstr, start_date, end_date);
                        break;
                    case 2:
                        P_NameArry = bufMySql.MysqlField_Seek("droduce_data", "p2_station");
                        if (checkBox1.Checked)
                            buf_datatable = bufMySql.MysqlDate_seek("p2_station", "生产日期", is_NameArry, start_date, end_date);
                        else
                            buf_datatable = bufMySql.MysqlDate_seek("p2_station", "生产日期", bufstr, start_date, end_date);
                        break;
                    case 3:
                        P_NameArry = bufMySql.MysqlField_Seek("droduce_data", "p3_station");
                        if (checkBox1.Checked)
                            buf_datatable = bufMySql.MysqlDate_seek("p3_station", "生产日期", is_NameArry, start_date, end_date);
                        else
                            buf_datatable = bufMySql.MysqlDate_seek("p3_station", "生产日期", bufstr, start_date, end_date);
                        break;
                    case 4:
                        P_NameArry = bufMySql.MysqlField_Seek("droduce_data", "p4_station");
                        if (checkBox1.Checked)
                            buf_datatable = bufMySql.MysqlDate_seek("p4_station", "生产日期", is_NameArry, start_date, end_date);
                        else
                            buf_datatable = bufMySql.MysqlDate_seek("p4_station", "生产日期", bufstr, start_date, end_date);
                        break;
                    case 5:
                        P_NameArry = bufMySql.MysqlField_Seek("droduce_data", "p5_station");
                        if (checkBox1.Checked)
                            buf_datatable = bufMySql.MysqlDate_seek("p5_station", "生产日期", is_NameArry, start_date, end_date);
                        else
                            buf_datatable = bufMySql.MysqlDate_seek("p5_station", "生产日期", bufstr, start_date, end_date);
                        break;
                    case 6:
                        P_NameArry = bufMySql.MysqlField_Seek("droduce_data", "p6_station");
                        if (checkBox1.Checked)
                            buf_datatable = bufMySql.MysqlDate_seek("p6_station", "生产日期", is_NameArry, start_date, end_date);
                        else
                            buf_datatable = bufMySql.MysqlDate_seek("p6_station", "生产日期", bufstr, start_date, end_date);
                        break;
                    case 7:
                        P_NameArry = bufMySql.MysqlField_Seek("droduce_data", "p7_station");
                        if (checkBox1.Checked)
                            buf_datatable = bufMySql.MysqlDate_seek("p7_station", "生产日期", is_NameArry, start_date, end_date);
                        else
                            buf_datatable = bufMySql.MysqlDate_seek("p7_station", "生产日期", bufstr, start_date, end_date);
                        break;
                    case 8:
                        P_NameArry = bufMySql.MysqlField_Seek("droduce_data", "p8_station");
                        if (checkBox1.Checked)
                            buf_datatable = bufMySql.MysqlDate_seek("p8_station", "生产日期", is_NameArry, start_date, end_date);
                        else
                            buf_datatable = bufMySql.MysqlDate_seek("p8_station", "生产日期", bufstr, start_date, end_date);
                        break;
                }

                //显示列名
                DataGridView_init2(dataGridView1, P_NameArry);
                //显示数据
                GridviewShowDataTable(buf_datatable, dataGridView1);
            }
            catch (Exception er)
            {
                MessageBox.Show("工位号错误/错误信息：" + er.ToString(), "提示");
            }
        }


        //导出excel报表
        private void export()
        {
            try
            {
                SaveFileDialog saveFileDialog = new SaveFileDialog();
                saveFileDialog.FilterIndex = 1;
                saveFileDialog.RestoreDirectory = true;
                saveFileDialog.Filter = "xlsx文件(*.xlsx)|*.xlsx";
                if (saveFileDialog.ShowDialog() == DialogResult.OK)
                {
                    if (saveFileDialog.FileName.ToString() == string.Empty) return;
                    string localFilePth = saveFileDialog.FileName.ToString();
                    string fileNameExt = localFilePth.Substring(localFilePth.LastIndexOf("\\") + 1);
                    string FilePath = localFilePth.Substring(0, localFilePth.LastIndexOf("\\"));
                    string newFileName = FilePath + "\\" + DateTime.Now.ToString("yyyyMMdd") + "_" + fileNameExt;


                    DataTable dt = DataGridViewTODataTable(dataGridView1);
                    if (dt != null && dt.Rows.Count > 0 && dt.Columns.Count > 0)
                    {
                        DataTableTOExcel(dt, newFileName);
                    }
                    else
                    {
                        MessageBox.Show("导出报表失败。", "提示");
                    }

                }

            }
            catch (Exception er)
            {
                MessageBox.Show("导出报表失败，错误信息：" + er.ToString(), "提示");
            }



        }

        private void timer1_Tick(object sender, EventArgs e)
        {
            dataGridView1.Update();
            dataGridView1.Refresh();
        }

        private void dataGridView1_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            for (int i = 0; i < this.dataGridView1.Rows.Count; i++)
            {
                DataGridViewRow r = this.dataGridView1.Rows[i];
                //r.HeaderCell.Value = string.Format("{0}", i + 1);
                r.Cells[0].Value = string.Format("{0}", i + 1);

            }
            this.dataGridView1.Refresh();
        }

        public void DataGridView_init(DataGridView dataGridView, string[] title_str, int[] title_width, bool Edit_state = false)
        {
            
            //dataGridView部分设置
            //禁止编辑后自动生成一行
            dataGridView.AllowUserToAddRows = false;
            //设置为系统表格样式
            //dataGridView.EnableHeadersVisualStyles = false;
            //自适应列宽
            //dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader;
            //dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
            //列头文本居中
            dataGridView.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;//列头文本居中

            //禁止显示列标题
            dataGridView.RowHeadersVisible = false;
            //行头边框样式                                                                                            
            //dataGridView.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
            //单元格文本居中
            dataGridView.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
            //自适应行高度
            //dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders;
            //禁止用户改变列宽行高
            //dataGridView.AllowUserToOrderColumns = false;
            //dataGridView.AllowUserToResizeRows = false;
            //dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            //dataGridView.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;

            dataGridView.AllowUserToDeleteRows = false;
            dataGridView.AllowUserToResizeColumns = false;
            dataGridView.AllowUserToResizeRows = false;

            //字体大小编辑
            dataGridView.ColumnHeadersDefaultCellStyle.Font = new Font("宋体", 10.5f, Font.Style | FontStyle.Regular);
            dataGridView.DefaultCellStyle.Font = new Font("宋体", 10.5f, Font.Style | FontStyle.Regular);


            //行头宽度
            //dataGridView.RowHeadersWidth = 15;

            //禁止编辑
            dataGridView.ReadOnly = Edit_state;

            //鼠标选定颜色
            dataGridView1.DefaultCellStyle.SelectionBackColor = SystemColors.Highlight;
            dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Black;

            //增加列头列名称
            dataGridView.ColumnCount = title_str.Length + 1;
            dataGridView.ColumnHeadersVisible = true;
            DataGridViewCellStyle code = new DataGridViewCellStyle();
            code.BackColor = Color.Beige;
            code.Font = new Font("Verdana", 10, FontStyle.Bold);

            if (title_str.Length > 0)
            {
                dataGridView.Columns[0].Name = "序号";
                dataGridView.Columns[0].Width = 50;
                dataGridView.Columns[0].ReadOnly = true;//第一列禁止编辑
                for (int i = 0; i < title_str.Length; i++)
                {
                    dataGridView1.Columns[i + 1].ReadOnly = true;//禁止编辑
                    dataGridView.Columns[i + 1].Name = title_str[i];
                    //dataGridView.Columns[i + 1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                    //dataGridView.Columns[i + 1].FillWeight = title_width[i];
                    dataGridView.Columns[i + 1].Width = title_width[i];
                }
            }


            //
            if (dataGridView.Columns.Count > 0)
            {
                for (int i = 0; i < dataGridView.Columns.Count; i++)
                {
                    dataGridView.Columns[i].SortMode = DataGridViewColumnSortMode.Programmatic;
                }
            }

        }
        public void DataGridView_init2(DataGridView dataGridView, string[] title_str, bool Edit_state = false)
        {

            //dataGridView部分设置
            //禁止编辑后自动生成一行
            dataGridView.AllowUserToAddRows = false;
            //设置为系统表格样式
            //dataGridView.EnableHeadersVisualStyles = false;
            //自适应列宽
            //dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.ColumnHeader;
            //dataGridView.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
            //列头文本居中
            dataGridView.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleCenter;//列头文本居中

            //禁止显示列标题
            dataGridView.RowHeadersVisible = false;
            //行头边框样式                                                                                            
            //dataGridView.RowHeadersBorderStyle = DataGridViewHeaderBorderStyle.Single;
            //单元格文本居中
            dataGridView.DefaultCellStyle.Alignment = DataGridViewContentAlignment.MiddleLeft;
            //自适应行高度
            //dataGridView1.AutoSizeRowsMode = DataGridViewAutoSizeRowsMode.AllCellsExceptHeaders;
            //禁止用户改变列宽行高
            //dataGridView.AllowUserToOrderColumns = false;
            //dataGridView.AllowUserToResizeRows = false;
            //dataGridView.ColumnHeadersHeightSizeMode = DataGridViewColumnHeadersHeightSizeMode.DisableResizing;
            //dataGridView.RowHeadersWidthSizeMode = DataGridViewRowHeadersWidthSizeMode.DisableResizing;

            dataGridView.AllowUserToDeleteRows = false;
            dataGridView.AllowUserToResizeColumns = false;
            dataGridView.AllowUserToResizeRows = false;

            //字体大小编辑
            dataGridView.ColumnHeadersDefaultCellStyle.Font = new Font("宋体", 10.5f, Font.Style | FontStyle.Regular);
            dataGridView.DefaultCellStyle.Font = new Font("宋体", 10.5f, Font.Style | FontStyle.Regular);


            //行头宽度
            //dataGridView.RowHeadersWidth = 15;

            //禁止编辑
            dataGridView.ReadOnly = Edit_state;

            //鼠标选定颜色
            dataGridView1.DefaultCellStyle.SelectionBackColor = SystemColors.Highlight;
            dataGridView1.DefaultCellStyle.SelectionForeColor = Color.Black;

            //增加列头列名称
            dataGridView.ColumnCount = title_str.Length + 1;
            dataGridView.ColumnHeadersVisible = true;
            DataGridViewCellStyle code = new DataGridViewCellStyle();
            code.BackColor = Color.Beige;
            code.Font = new Font("Verdana", 10, FontStyle.Bold);

            if (title_str.Length > 0)
            {
                dataGridView.Columns[0].Name = "序号";
                dataGridView.Columns[0].Width = 50;
                dataGridView.Columns[0].ReadOnly = true;//第一列禁止编辑
                for (int i = 0; i < title_str.Length; i++)
                {
                    dataGridView1.Columns[i + 1].ReadOnly = true;//禁止编辑
                    dataGridView.Columns[i + 1].Name = title_str[i];
                    //dataGridView.Columns[i + 1].AutoSizeMode = DataGridViewAutoSizeColumnMode.Fill;
                    //dataGridView.Columns[i + 1].FillWeight = title_width[i];
                    dataGridView.Columns[i + 1].Width = 200;
                }
            }


            //
            if (dataGridView.Columns.Count > 0)
            {
                for (int i = 0; i < dataGridView.Columns.Count; i++)
                {
                    dataGridView.Columns[i].SortMode = DataGridViewColumnSortMode.Programmatic;
                }
            }

        }
        public void DataGridView(string[] str_name,string[] str_value)
        {
            dataGridView1.Rows.Clear();
            for (int h = 0; h < 1; h++)
            {
                for (int i = 0; i < str_name.Length; i++)
                {
                    if (h < 1)
                    { dataGridView1.Rows.Add(); }

                    string value1 = "";
                    string value2 = "";
                    try { value1 = (str_name != null && str_name[i] != null) ? str_name[i] : ""; } catch { value1 = ""; };
                    try { value2 = (str_value != null && str_value[i] != null) ? str_value[i] : ""; } catch { value2 = ""; };
                    dataGridView1.Rows[i].Cells[1].Value = value1;
                    dataGridView1.Rows[i].Cells[2].Value = value2;
                }
            }
        }

        public void DataGridView2(string[] str_name, string[] str_value)
        {
            dataGridView1.Rows.Clear();
            for (int h = 0; h < 1; h++)
            {
                for (int i = 0; i < 1; i++)
                {
                    if (h < 1)
                    { dataGridView1.Rows.Add(); }

                    string value1 = "";
                    string value2 = "";
                    try { value1 = (str_value != null && str_value[i + 1] != null) ? str_value[i + 1] : ""; } catch { value1 = ""; };
                    try { value2 = (str_value != null && str_value[i + 2] != null) ? str_value[i + 2] : ""; } catch { value2 = ""; };
                    dataGridView1.Rows[i].Cells[1].Value = value1;
                    dataGridView1.Rows[i].Cells[2].Value = value2;
                }
            }
        }
        public int GridviewShowDataTable(DataTable table, DataGridView dataGridView)
        {
            try
            {
                dataGridView1.Rows.Clear();
                if (table.Rows.Count > 0)
                {
                    for (int i = 0; i < table.Rows.Count; i++)
                    {
                        try
                        {
                            dataGridView.Rows.Add();
                            dataGridView.Rows[i].Cells[0].Value = i;
                            for (int j = 0; j < (table.Columns.Count - 0); j++)
                            {
                                dataGridView.Rows[i].Cells[j + 1].Value = table.Rows[i].ItemArray[j + 0].ToString();
                                //Application.DoEvents();//可以交出控制权但是慢
                            }
                        }
                        catch {; }
                    }
                }

                return 0;
            }
            catch (Exception ex)
            { return 1; }

        }

        //
        private void DataTableTOExcel(DataTable tableA, string path)
        {
            try
            {

                FileInfo file = new FileInfo(path);
                using (ExcelPackage pack = new ExcelPackage(file))
                {
                    ExcelWorksheet w = pack.Workbook.Worksheets["sheet"];
                    if (w != null && w.Name.Equals("sheet")) //判断是否存在该sheet表，存在则删除
                        pack.Workbook.Worksheets.Delete(w);
                    ExcelWorksheet ws = pack.Workbook.Worksheets.Add("sheet");
                    ws.Cells["A1"].LoadFromDataTable(tableA, true); //第二个参数设置为true则显示datable表头
                    pack.Save();
                }


            }
            catch (Exception)
            {

                throw;
            }
        }

        //
        private DataTable DataGridViewTODataTable(DataGridView tableA)
        {
            try
            {
                //DataTable dt = (dataGridView5.DataSource as DataTable);//绑定数据源的情况下

                DataTable dt = new DataTable();

                // 列强制转换
                for (int count = 0; count < tableA.Columns.Count; count++)
                {
                    DataColumn dc = new DataColumn(tableA.Columns[count].Name.ToString());
                    dt.Columns.Add(dc);
                }

                // 循环行
                for (int count = 0; count < tableA.Rows.Count; count++)
                {
                    DataRow dr = dt.NewRow();
                    for (int countsub = 0; countsub < tableA.Columns.Count; countsub++)
                    {
                        dr[countsub] = Convert.ToString(tableA.Rows[count].Cells[countsub].Value);
                    }
                    dt.Rows.Add(dr);
                }

                return dt;
            }
            catch (Exception er)
            {
                return null;
            }
        }


    }
}
